<template>
  <div id="home">
    <!-- 路由动画 -->
    <transition :name="transitionName">
      <!-- 组件缓存 -->
      <keep-alive>
        <router-view class="child-view" />
      </keep-alive>
    </transition>
    <van-tabbar active-color="#e40137" inactive-color="#b4b4bd" route fixed>
      <van-tabbar-item replace to="/home/compony">
        <template #icon>
          <i class="iconfont iconicon_footbar_gs_nor"></i>
        </template>
        公司
      </van-tabbar-item>
      <van-tabbar-item replace to="/home/subject">
        <template #icon>
          <i class="iconfont iconicon_footbar_st_nor"></i> </template
        >刷题</van-tabbar-item
      >
      <van-tabbar-item replace to="/home/find">
        <template #icon>
          <i class="iconfont iconicon_footbar_fx_sel"></i> </template
        >发现</van-tabbar-item
      >
      <van-tabbar-item replace to="/home/my">
        <template #icon>
          <i class="iconfont iconicon_footbar_wode_nor"></i> </template
        >我的</van-tabbar-item
      >
    </van-tabbar>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { getLocal } from '@/utils/local.js'
export default {
  created () {
    if (getLocal()) {
      this.setUserInfo()
    }
  },
  methods: {
    ...mapActions(['setUserInfo'])
  },
  watch: {
    $route (to, from) {
      console.log(to)
      console.log(from)
      if (to.meta.index > from.meta.index) {
        // 点击右边向左滑动
        this.transitionName = 'slide-left'
      } else {
        // 点击左边向右滑动
        this.transitionName = 'slide-right'
      }
    }
  },
  data () {
    return {
      transitionName: 'slide-left'
    }
  }
}
</script>

<style lang="less" scoped>
#home {
  // 必须给父容器固定住,防止切换动画时撑大容器
  position: absolute;
  top: 0px;
  bottom: 0px;
  width: 100%;
  overflow: auto;
  /deep/ .iconfont {
    font-size: 28px;
  }
  .child-view {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 50px;
    right: 0;
    transition: all 0.4s;
  }
  // 滑动样式
  .slide-left-enter,
  .slide-right-leave-active {
    opacity: 0;
    -webkit-transform: translate(100%, 0);
    transform: translate(100%, 0);
  }
  .slide-left-leave-active,
  .slide-right-enter {
    opacity: 0;
    -webkit-transform: translate(-100%, 0);
    transform: translate(-100%, 0);
  }
}
</style>
